import { Resolver, Query, Context, ResolveProperty, Parent } from '@nestjs/graphql';
import { Menu } from './menu.entity';
import { UseGuards } from '@nestjs/common';
import { JwtAuthGuard } from '../auth/jwt-auth.guard';
import { AdminUser } from '../admin-user/admin-user.entity';
import { MenuService } from './menu.service';

@Resolver(of => Menu)
export class MenuResolver {

  constructor(private readonly service: MenuService) { }

  @Query(returns => [Menu], { name: 'userMenuData', nullable: true })
  @UseGuards(JwtAuthGuard)
  async getUserMenuData(@Context() context) {
    const user: AdminUser = context.req.user.user;
    const menus = await this.service.getMenuDataByUserId(user.id);
    return menus;
  }

  @ResolveProperty('subMenu', returns => [Menu], { nullable: true, name: 'subMenu' })
  async getSubMenu(@Parent() menu) {
    if (!menu.isLeaf) {
      return await this.service.getSubMenu(menu);
    } else {
      return [];
    }
  }
}
